package detect.parse.tree;

import detect.Log;
import detect.parse.tree.token.RawTokenType;
import detect.parse.tree.token.TokenList;

public class CaseExpression extends Expression{
	public Expression targetValueExp=null;
	public BlockExpression caseBlockExp=null;
	
	public TreeType getType(){
		return TreeType.CaseType;
	}
	public CaseExpression(){
		
	}	
	
	@Override
	public void findInnerPatterns(){
		caseBlockExp.findInnerPatterns();
		patterns=caseBlockExp.patterns;
	}
	
	public CaseExpression(TokenList _tokenList,Expression _targetValueExp,BlockExpression _caseBlockExp){
		tokenList=_tokenList;
		targetValueExp=_targetValueExp;
		caseBlockExp=_caseBlockExp;
	}
	
	@Override
	public Expression getExpression(TokenList _tokenList){
		tokenList=_tokenList;
		Log.incIndence();
		Log.printTree(this.getType().toString());
		int colonPos=tokenList.indexOf(RawTokenType.COLON);
		if(colonPos==-1){
			Log.error("no ':' found after 'case'",tokenList.toString());
			return null;
		}
		
		TokenList targetValueTokenList=tokenList.subList(1, colonPos);
		TokenList caseBodyTokenList=tokenList.subList(colonPos+1);
		
		Expression _targetValueExp=ExpressionFactory.expInstance.getExpression(targetValueTokenList);
		Expression _caseBlockExp=ExpressionFactory.blockExpInstance.getExpression(caseBodyTokenList);
		
		Log.decIndence();
		return new CaseExpression(_tokenList,_targetValueExp,(BlockExpression)_caseBlockExp);
		
	}
}
